package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.JSONUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @PostMapping("/save")
    @ResponseBody
    public Object save(Travel travel) {
        JSONResult json = new JSONResult();
        try {
             travel.setAuthor(UserContext.getCurrentUser());
            travelService.saveOrUpdate(travel);
        }catch (Exception e){
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }


    @GetMapping("/{id}")
    @ResponseBody
    public Object getTravel(@PathVariable Long id) {
        Travel travel = travelService.selectByPrimaryKey(id);

        return travel;
    }


    //查询所有公开的游记
    @GetMapping
    @ResponseBody
    public Object getTravels(TravelQueryObject qo) {
        //设置查询条件,公开的
        qo.setIsPublic(true);
        //设置查询条件,已发布的
        qo.setState(3);
        qo.setOrderBy("t.releaseTime desc");
        PageInfo pageInfo = travelService.getList(qo);
        return  pageInfo;
    }

    @GetMapping("/{authorId}/author")
    @ResponseBody
    public Object travelList(TravelQuery qo) {

        return travelService.getTravels(qo);
    }

    @GetMapping("/{authorId}/count")
    @ResponseBody
    public Object travelCount(@PathVariable Long authorId) {

        return travelService.getTravelCount(authorId);
    }

}
